package member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class Login {
	private String mid = "";
	private String pwd = "";
	private String newPwd1="";


	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";

	Connection conn;

	public Login() {
		try {

			// 1)드라이버 로딩
			Class.forName(driver).newInstance();

			// 2)Connection 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);
		} catch (Exception ex) {
			ex.getStackTrace();
		}
	}

	public boolean login() {
		boolean flag=false;
		try {
			String sql = "select * from memberdata where memberid=? and pwd=?";
			// 3)PreparedStatement 작성
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);

			// 4)ResultSet생성
			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				flag = true;
			} else {
				flag = false;
			}
			
			rs.close();
			ps.close();
			conn.close();

		} catch (Exception ex) {
			ex.getSuppressed();
		}
		return flag;
	}
	
	public Vector<MemberVo> list(){
		Vector<MemberVo> v= new Vector<>();
		String sql="select * from memberdata member where memberid like ? order by memberid";
			try{
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, "%"+mid+"%");
				ResultSet rs= ps.executeQuery();
				
				while(rs.next()){
					MemberVo m= new MemberVo(rs.getString("memberid"), rs.getString("pwd"),rs.getString("irum"),rs.getString("phone"));
					v.add(m);
					
				}
				rs.close();
				ps.close();
				conn.close();
				
			}catch(Exception ex){
				ex.getStackTrace();
				
			}
		
		return v;
	}
	
	public MemberVo view(){
		MemberVo m=null;
		String sql= "select * from memberdata where memberid=?";
		try{
			
			PreparedStatement ps= conn.prepareStatement(sql);
			ps.setString(1,mid);
			ResultSet rs=ps.executeQuery();
			
			if(rs.next()){
				m= new MemberVo(rs.getString("memberid"), rs.getString("pwd"),rs.getString("irum"),rs.getString("phone"));
				
			}
			
		}catch(Exception ex){
			ex.getStackTrace();
		}
		
		
		return m;
	}
	
	public boolean modify(){
		boolean flag=false;
		String sql="update memberdata set pwd=? where memberid=? and pwd=?";
		
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, newPwd1);
			ps.setString(2, mid);
			ps.setString(3, pwd);
			
			//sql문 실행시 cnt값을 1로반환
			int cnt=ps.executeUpdate();
			if(cnt>0) flag=true;
			
			ps.close();
			conn.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
			
		}
		return flag;
	}
	
	public boolean delete(){
		boolean flag=false;
		String sql="delete from memberdata where memberid=?";
		
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			
			//sql문 실행시 cnt값을 1로반환
			int cnt=ps.executeUpdate();
			if(cnt>0) flag=true;
			
			ps.close();
			conn.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
			
		}
		return flag;
	}
	public boolean join(){
		boolean flag= false;
		
		String sql="insert into memberdata(memberid,pwd) values(?,?)";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);
			int cnt=ps.executeUpdate();
			if(cnt>0) flag=true;
			
			ps.close();
			conn.close();
			
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
		return flag;
	}
	
	

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNewPwd1() {
		return newPwd1;
	}

	public void setNewPwd1(String newPwd1) {
		this.newPwd1 = newPwd1;
	}
}
